package com.ws.service;

import com.ws.bean.Customer;

public class CustomerList {

    private Customer[] customers;//用来保存客户的数组
    private int total = 0;//记录以已保存客户对象的数量

    /**
     * 用来初始化 customers 数组的构造器
     * @param total:指定数组的长度
     */
    public CustomerList(int total) {
        customers = new Customer[total];
    }

    /**
     * @Description 将指定的客户添加到数组中
     * @param customer
     * @return true：成功；false：失败
     */
    public boolean addCustomer(Customer customer) {
        if (total >= customers.length) {
            return false;
        }
        customers[total++] = customer;
        return true;
    }

    /**
     * @Description 修改指定索引位置的客户信息
     * @param index 索引
     * @param customer 客户信息
     * @return true：成功；false：失败
     */
    public boolean replaceCustomer(int index, Customer customer) {
        if (index < 0 || index >= total) {
            return false;
        }
        customers[index] = customer;
        return true;
    }

    /**
     * @Description 删除指定索引位置的客户
     * @param index 索引
     * @return true：成功；false：失败
     */
    public boolean  deleteCustomer(int index) {
        if (index < 0 || index >= total) {
            return false;
        }
        for (int i = index; i < total - 1; i++) {
            customers[index] = customers[i+1];
        }
        //最后有数据的元素需要置空
//        customers[total - 1] = null;
//        total--;
        customers[--total] = null;
        return true;
    }

    /**
     * @Description 获取所有客户信息
     * @return 客户信息
     */
    public Customer[] getCustomers() {

        Customer[] custs = new Customer[total];

        for (int i = 0; i < total; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }

    /**
     * @Description 获取指定索引位置上的客户
     * @param index
     * @return
     */
    public Customer getCustomer(int index) {
        if (index < 0 || index >= total) {
            return null;
        }
        return customers[index];
    }

    /**
     * @Description 获取存储客户的数量
     * @return
     */
    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

}
